<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/11/9 0009
 * Time: 14:02
 */

namespace app\index\controller;
use think\Controller;

class Common extends Controller
{
	/**
	 * 查询快递
	 */
	public function express(){
		$host = "https://goexpress.market.alicloudapi.com";
		$path = "/goexpress";
		$method = "GET";
		$appcode = "0719cd96562549b3b0a8a0f726ead81a";
		$headers = array();
		array_push($headers, "Authorization:APPCODE " . $appcode);
		$querys = "no=802127881394244782";
		$bodys = "";
		$url = $host . $path . "?" . $querys;

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, false);
		//curl_setopt($curl, CURLOPT_HEADER, true); 如不输出json, 请打开这行代码，打印调试头部状态码。
		//状态码: 200 正常；400 URL无效；401 appCode错误； 403 次数用完； 500 API网管错误
		if (1 == strpos("$".$host, "https://"))
		{
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		$data = json_decode(curl_exec($curl),true);
		var_dump($data);
	}


	/**
	 * 天气查询
	 * @param string $city
	 */
	public function weather($city=''){
		$city = '广州';
		$host = "https://saweather.market.alicloudapi.com";
		$path = "/spot-to-weather";
		$method = "GET";
		$appcode = "0719cd96562549b3b0a8a0f726ead81a";
		$headers = array();
		array_push($headers, "Authorization:APPCODE " . $appcode);
		$querys = "area=".$city."&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=1&needMoreDay=1";
		$bodys = "";
		$url = $host . $path . "?" . $querys;

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, false);
		if (1 == strpos("$".$host, "https://"))
		{
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		$data = (json_decode(curl_exec($curl),true));
		var_dump($data);
	}


	/**
	 * 获取IP
	 */
	public function getIp(){
		if ($_SERVER['REMOTE_ADDR']) {//判断SERVER里面有没有ip，因为用户访问的时候会自动给你网这里面存入一个ip
			$cip = $_SERVER['REMOTE_ADDR'];
		} elseif (getenv("REMOTE_ADDR")) {//如果没有去系统变量里面取一次 getenv()取系统变量的方法名字
			$cip = getenv("REMOTE_ADDR");
		} elseif (getenv("HTTP_CLIENT_IP")) {//如果还没有在去系统变量里取下客户端的ip
			$cip = getenv("HTTP_CLIENT_IP");
		} else {
			$cip = "unknown";
		}
		echo $cip;
	}
}